package logic

import (
	"MyBlogv2/Servers/UserAction/internal/svc"
	"MyBlogv2/Servers/UserAction/userAction"
	"MyBlogv2/blog-common/HttpCode"
	"MyBlogv2/blog-common/models"
	"context"

	"github.com/zeromicro/go-zero/core/logx"
)

type IsFavoriteLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewIsFavoriteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *IsFavoriteLogic {
	return &IsFavoriteLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *IsFavoriteLogic) IsFavorite(in *UserAction.UserActionRequest) (*UserAction.IsFavoriteResponse, error) {
	typeOfFavorite := int(in.Type)
	typeId := int(in.TypeId)
	userId := int(in.UserId)
	var dbCount int64
	models.Db.Model(new(models.Favorite)).Where("user_id = ? and type = ? and type_id = ?", userId, typeOfFavorite, typeId).Count(&dbCount)
	if dbCount > 0 {
		return &UserAction.IsFavoriteResponse{
			Code: int64(HttpCode.SUCCESS),
			Data: true,
		}, nil
	}
	return &UserAction.IsFavoriteResponse{
		Code: int64(HttpCode.SUCCESS),
		Data: false,
	}, nil

}
